﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

[System.Serializable]
public class SpriteInfo
{
	public Rect rect;
    public Vector4 border; //{x = left, y = bottom, z = right, w = top}
	public Vector2 pivod;
	public Vector4 padding; //{x = left, y = bottom, z = right, w = top}

	public System.WeakReference spriteRef;

	public bool isValid()
	{
		if(rect.width == 0 || rect.height == 0)
		{
			return false;
		}
		return true;
	}

	public void Write(BinaryWriter writer)
	{
		writer.Write(rect.x);
		writer.Write(rect.y);
		writer.Write(rect.width);
		writer.Write(rect.height);

		writer.Write(border[0]);
		writer.Write(border[1]);
		writer.Write(border[2]);
		writer.Write(border[3]);

		writer.Write(pivod[0]);
		writer.Write(pivod[1]);

		writer.Write(padding[0]);
		writer.Write(padding[1]);
		writer.Write(padding[2]);
		writer.Write(padding[3]);
	}

	public void Write(StringWriter writer)
	{
		writer.Write(rect.x);
		writer.Write(rect.y);
		writer.Write(rect.width);
		writer.Write(rect.height);
		
		writer.Write(border[0]);
		writer.Write(border[1]);
		writer.Write(border[2]);
		writer.Write(border[3]);
		
		writer.Write(pivod[0]);
		writer.Write(pivod[1]);
		
		writer.Write(padding[0]);
		writer.Write(padding[1]);
		writer.Write(padding[2]);
		writer.Write(padding[3]);
	}

	public void Read(BinaryReader reader)
	{
		rect.x = reader.ReadSingle();
		rect.y = reader.ReadSingle();
		rect.width = reader.ReadSingle();
		rect.height = reader.ReadSingle();
		
		border[0] = reader.ReadSingle();
		border[1] = reader.ReadSingle();
		border[2] = reader.ReadSingle();
		border[3] = reader.ReadSingle();
		
		pivod[0] = reader.ReadSingle();
		pivod[1] = reader.ReadSingle();

		padding[0] = reader.ReadSingle();
		padding[1] = reader.ReadSingle();
		padding[2] = reader.ReadSingle();
		padding[3] = reader.ReadSingle();
	}

	public Rect getOutterSpriteRect()
	{
		Rect temp = this.rect;
		temp.x -= padding.x;
		temp.y -= padding.y;
		temp.width += padding.x + padding.z;
		temp.height += padding.y + padding.w;
		return temp;
	}
}

